package yanc.app

import grails.converters.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.text.SimpleDateFormat

class PayOrderController {
    def payOrderService
    static final Logger logger = LoggerFactory.getLogger(PayOrderController.class)

    def get() {
        def data = [:]
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd")
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def orderNo = "${params.orderNo ?: ''}"
        def startime = "${params.transactionDate1 ?: ''}"
        def endtime = "${params.transactionDate2 ?: ''}"
        def a = PayOrder.createCriteria()
        def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
            and {
                if (!orderNo.isEmpty()) {
                    eq("orderNo", orderNo)
                }
                if (!startime.isEmpty()) {
                    ge("transactionDate", new Date(Long.parseLong(startime)))
                }
                if (!endtime.isEmpty()) {
                    def start = sdf2.format(new Date(Long.parseLong(endtime)))
                    Date end = sdf1.parse(start + " 23:59:59")
                    le("transactionDate", end)
                }
            }
            order("transactionDate", "desc")
        }
        def b = PayOrder.createCriteria()
        def length = b.get {
            and {
                if (!orderNo.isEmpty()) {
                    eq("orderNo", orderNo)
                }
                if (!startime.isEmpty()) {
                    ge("transactionDate", new Date(Long.parseLong(startime)))
                }
                if (!endtime.isEmpty()) {
                    def start = sdf2.format(new Date(Long.parseLong(endtime)))
                    Date end = sdf1.parse(start + " 23:59:59")
                    le("transactionDate", end)
                }
            }
            projections {
                rowCount()
            }
        }
        if (list) {
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON
    }

    def refund() {
        def data = [:]
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def orderNo = "${params.orderNo ?: ''}"
        def reason = "${params.reason ?: ''}"
        def list = []
        def length = 0
        try {
            def payOrder = PayOrder.findByOrderNo(orderNo)
            def flag = payOrderService.refund(payOrder.orderNo, payOrder.merchantOrderNo, payOrder.orderTotalAmount, reason)
            if (flag) {
                list = PayOrder.list(max: pagesize, offset: (page - 1) * pagesize, sort: "transactionDate", order: "desc")
                length = PayOrder.count()
                data << [statuCode: "0", errMsg: "0", data: list, total: length]
            } else {
                data << [statuCode: "1", errMsg: "1"]
            }
            render data as JSON
        } catch (Exception e) {
            logger.error("PayOrderController refund Exception" + e.message)
        }
    }

    def refresh() {
        def data = [:]
        Date date = new Date(Long.parseLong("${params.updateDate ?: ''}"))
        int day = (date.getTime() - new Date().getTime()) / (24 * 60 * 60 * 1000)
        try {
            def flag = payOrderService.download(day)
            def list = payOrderService.queryByDate(date, "1")
            if (!list) {
                data << [statuCode: "1", errMsg: "1"]
            } else {
                payOrderService.refresh(list)
                data << [statuCode: "0", errMsg: "0"]

            }
            render data as JSON
        } catch (Exception e) {
            logger.error("PayOrderController refresh Exception" + e.message)
        }
    }
}
